package com.runrunfast.fxj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runrunfast.fxj.entity.SysResources;
import com.runrunfast.fxj.entity.SysRole;
import com.runrunfast.fxj.entity.SysUser;
import com.runrunfast.fxj.enums.ResourcesEnum;
import com.runrunfast.fxj.enums.RoleStatusEnum;
import com.runrunfast.fxj.enums.UserStatusEnum;
import com.runrunfast.fxj.exception.FxjException;
import com.runrunfast.fxj.service.ISysRoleService;
import com.runrunfast.fxj.utils.ResultVOUtil;
import com.runrunfast.fxj.vo.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 陈胤训
 * @since 2019-03-12
 */
@Slf4j
@RestController
@Api(value = "后台角色接口",tags = {"后台角色接口"})
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @ApiResponses({
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "成功返回的records", response = SysRoleVo.class)
    })
    @ApiOperation("角色列表")
    @GetMapping("/list")
    public ResultVo list(PageVo pageVo,@RequestParam(value = "roleName",required = false) @ApiParam("根据名称模糊查询,为空查询全部") String description){
        PageVo<SysRoleVo> pageUserList = roleService.getPageUserList(pageVo,description);
        return ResultVOUtil.success(pageUserList);
    }

    @ApiOperation("角色添加")
    @PostMapping("/add")
    public ResultVo add(@ApiParam(name="roleVo",value="角色对象",required=true) @Validated SysRoleAddVo roleVo
            , BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            log.error("【角色添加】参数不正确, orderForm={}", roleVo);
            log.error(bindingResult.getFieldError().getDefaultMessage());
            throw new FxjException(5,bindingResult.getFieldError().getDefaultMessage());
        }
        if (StringUtils.isEmpty(roleVo)){
            throw new FxjException(RoleStatusEnum.ROLE_NULL.getCode(),RoleStatusEnum.ROLE_NULL.getDesc());
        }
        Integer count = roleService.count(new QueryWrapper<SysRole>().eq("role_name", roleVo.getRoleName()));
        if (count>0){
            throw new FxjException(RoleStatusEnum.ROLE_EXIST.getCode(),RoleStatusEnum.ROLE_EXIST.getDesc());
        }

        if(!roleService.add(roleVo)){
            throw new FxjException(RoleStatusEnum.ROLE_FAILED.getCode(),RoleStatusEnum.ROLE_FAILED.getDesc());
        }
        return ResultVOUtil.success();
    }

    @ApiOperation("修改角色信息")
    @PostMapping("/update")
    public ResultVo update(SysRoleVo roleVo){
        if (roleService.updateInfo(roleVo)){
            throw new FxjException(RoleStatusEnum.ROLE_NULL.getCode(),RoleStatusEnum.ROLE_NULL.getDesc());
        }
        return ResultVOUtil.success();
    }

    @ApiOperation("用户修改界面")
    @GetMapping("/updatePage")
    public ResultVo updatePage(@RequestParam("userId") Integer roleId){
        SysRole byId = roleService.getById(roleId);
        if (StringUtils.isEmpty(byId)){
            throw new FxjException(RoleStatusEnum.ROLE_NULL.getCode(),RoleStatusEnum.ROLE_NULL.getDesc());
        }
        return ResultVOUtil.success(byId);
    }

    @ApiOperation("删除角色")
    @PostMapping("/deployRoleResources")
    public ResultVo deployRoleResources(@RequestParam(value = "roleId") @ApiParam("角色Id") Integer roleId
            , @RequestParam(value = "ids",required = false) @ApiParam("权限id集合 以 , 分割id  格式 1,2,3,4,5")String ids){
        SysRole byId = roleService.getById(roleId);
        if (StringUtils.isEmpty(byId)){
            throw new FxjException(RoleStatusEnum.ROLE_NULL.getCode(),RoleStatusEnum.ROLE_NULL.getDesc());
        }
        roleService.deployRoleResources(roleId,ids);
        return ResultVOUtil.success();
    }

    @ApiOperation("删除角色 没实现1")
    @DeleteMapping("delete")
    public ResultVo delete(@RequestParam("resourcesId") @ApiParam("权限id") Integer resourcesId){

        return ResultVOUtil.success();
    }
}
